﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    public class VCWindowPending : EditorWindow
    {
        private Texture2D changeIcon;
        private GUIStyle header;
        [SerializeField]
        private VCListControl incomingList;
        [SerializeField]
        private VCListControl pendingList;
        private Texture2D refreshIcon;
        private Texture2D submittedIcon;
        private Texture2D syncIcon;

        private void CreateResources()
        {
            if (this.refreshIcon == null)
            {
                this.refreshIcon = EditorGUIUtility.LoadIcon("Refresh");
            }
            if (this.syncIcon == null)
            {
                this.syncIcon = EditorGUIUtility.LoadIcon("vcs_sync");
            }
            if (this.changeIcon == null)
            {
                this.changeIcon = EditorGUIUtility.LoadIcon("vcs_change");
            }
            if (this.submittedIcon == null)
            {
                this.submittedIcon = EditorGUIUtility.LoadIcon("vcs_submitted");
            }
            if (this.header == null)
            {
                this.header = "OL Title";
            }
        }

        private void OnChangeContents(VCTask task)
        {
            VCListItem user = task.user as VCListItem;
            if (user != null)
            {
                user.RemoveAll();
                if (task.assetList.Count == 0)
                {
                    this.pendingList.Add(user, "Empty change list", (VCAsset) null).Dummy = true;
                }
                else
                {
                    foreach (VCAsset asset in task.assetList)
                    {
                        this.pendingList.Add(user, asset.prettyPath, asset);
                    }
                }
                this.pendingList.Refresh(false);
                base.Repaint();
            }
        }

        private void OnChangeLists(VCTask task)
        {
            this.PopulateListControl(this.pendingList, task, this.changeIcon);
        }

        private void OnDisable()
        {
            VCDatabase.onInvalidate = (System.Action) Delegate.Remove(VCDatabase.onInvalidate, new System.Action(this.OnInvalidate));
        }

        private void OnDrop(VCChangelist targetItem)
        {
            VCAssetList selectedAssets = this.pendingList.SelectedAssets;
            VCProvider.ChangeMove(selectedAssets, targetItem);
            VCDatabase.Invalidate(selectedAssets);
        }

        private void OnEnable()
        {
            if (this.pendingList == null)
            {
                this.pendingList = new VCListControl();
            }
            this.pendingList.ExpandEvent = (VCListControl.ExpandDelegate) Delegate.Combine(this.pendingList.ExpandEvent, new VCListControl.ExpandDelegate(this.OnExpand));
            this.pendingList.DragEvent = (VCListControl.DragDelegate) Delegate.Combine(this.pendingList.DragEvent, new VCListControl.DragDelegate(this.OnDrop));
            this.pendingList.MenuDefault = "CONTEXT/Pending";
            this.pendingList.MenuFolder = "CONTEXT/Change";
            this.pendingList.DragAcceptOnly = true;
            if (this.incomingList == null)
            {
                this.incomingList = new VCListControl();
            }
            this.incomingList.ExpandEvent = (VCListControl.ExpandDelegate) Delegate.Combine(this.incomingList.ExpandEvent, new VCListControl.ExpandDelegate(this.OnExpandIncoming));
            VCDatabase.onInvalidate = (System.Action) Delegate.Combine(VCDatabase.onInvalidate, new System.Action(this.OnInvalidate));
            VCDatabase.Invalidate();
        }

        private void OnExpand(VCChangelist change, VCListItem item)
        {
            if (VCProvider.isActive)
            {
                VCTask task = VCProvider.ChangeStatus(change);
                task.user = item;
                task.OnDone += new VCTask.DoneDelegate(this.OnChangeContents);
                if (!item.HasChildren)
                {
                    VCAsset asset = new VCAsset("Updating...");
                    this.pendingList.Add(item, asset.prettyPath, asset).Dummy = true;
                    this.pendingList.Refresh(false);
                    base.Repaint();
                }
            }
        }

        private void OnExpandIncoming(VCChangelist change, VCListItem item)
        {
            if (VCProvider.isActive)
            {
                VCTask task = VCProvider.IncomingChangeAssets(change);
                task.user = item;
                task.OnDone += new VCTask.DoneDelegate(this.OnChangeContents);
                if (!item.HasChildren)
                {
                    VCAsset asset = new VCAsset("Updating...");
                    this.incomingList.Add(item, asset.prettyPath, asset).Dummy = true;
                    this.incomingList.Refresh(false);
                    base.Repaint();
                }
            }
        }

        private void OnGetChanges(VCTask task)
        {
            VCDatabase.Invalidate();
            AssetDatabase.Refresh();
        }

        private void OnGUI()
        {
            this.CreateResources();
            Event current = Event.current;
            GUILayout.BeginHorizontal(EditorStyles.toolbar, new GUILayoutOption[0]);
            EditorGUI.BeginDisabledGroup(!VCProvider.isActive);
            if (GUILayout.Button(this.refreshIcon, EditorStyles.toolbarButton, new GUILayoutOption[0]))
            {
                VCDatabase.Invalidate();
            }
            if ((current.isKey && (GUIUtility.keyboardControl == 0)) && ((current.type == EventType.KeyDown) && (current.keyCode == KeyCode.F5)))
            {
                VCDatabase.Invalidate();
                current.Use();
            }
            EditorGUI.EndDisabledGroup();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Settings", EditorStyles.toolbarButton, new GUILayoutOption[0]))
            {
                EditorApplication.ExecuteMenuItem("Edit/Project Settings/Editor");
                EditorWindow.FocusWindowIfItsOpen<InspectorWindow>();
                GUIUtility.ExitGUI();
            }
            GUILayout.EndHorizontal();
            if (!VCProvider.isActive)
            {
                Color color = GUI.color;
                GUI.color = new Color(0.8f, 0.5f, 0.5f);
                GUILayout.BeginHorizontal(EditorStyles.toolbar, new GUILayoutOption[0]);
                GUILayout.FlexibleSpace();
                GUILayout.Label(VCProvider.enabled ? "OFFLINE" : "DISABLED", EditorStyles.miniLabel, new GUILayoutOption[0]);
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                GUI.color = color;
                base.Repaint();
            }
            else
            {
                bool flag = false;
                Rect position = new Rect(0f, 17f, base.position.width, 17f);
                GUI.Label(position, "Incoming Changelists", this.header);
                position.y += position.height;
                position.height = 200f;
                flag |= this.incomingList.OnGUI(position, base.hasFocus);
                position.y += position.height;
                position.height = 26f;
                GUIContent content = new GUIContent("Apply Incoming Changes");
                Vector2 vector = EditorStyles.miniButton.CalcSize(content);
                Rect rect2 = position;
                rect2.width = vector.x;
                rect2.height = vector.y;
                rect2.y = position.y + 7f;
                rect2.x = (position.xMax - vector.x) - 5f;
                EditorGUI.BeginDisabledGroup(this.incomingList.Size == 0);
                if (GUI.Button(rect2, content, EditorStyles.miniButton))
                {
                    VCAsset asset = new VCAsset(VCPath.Project);
                    VCProvider.GetLatest(new VCAssetList { asset }).OnDone += new VCTask.DoneDelegate(this.OnGetChanges);
                }
                EditorGUI.EndDisabledGroup();
                position.y += position.height;
                position.height = 17f;
                GUI.Label(position, "Pending Changelists", this.header);
                position.y += position.height;
                position.height = (base.position.height - 251f) - vector.y;
                if (flag | this.pendingList.OnGUI(position, base.hasFocus))
                {
                    base.Repaint();
                }
            }
        }

        private void OnIncoming(VCTask task)
        {
            this.PopulateListControl(this.incomingList, task, this.submittedIcon);
        }

        private void OnInvalidate()
        {
            if (!VCProvider.isActive)
            {
                this.pendingList.Clear();
                base.Repaint();
            }
            else
            {
                VCProvider.ChangeLists().OnDone += new VCTask.DoneDelegate(this.OnChangeLists);
                VCProvider.Incoming().OnDone += new VCTask.DoneDelegate(this.OnIncoming);
            }
        }

        private void OnProjectChange()
        {
            VCDatabase.Invalidate();
        }

        public void OnSelectionChange()
        {
            if (!base.hasFocus)
            {
                this.pendingList.Sync();
                base.Repaint();
            }
        }

        private void PopulateListControl(VCListControl list, VCTask task, Texture2D icon)
        {
            list.Clear();
            foreach (VCChangelist changelist in task.changesList)
            {
                VCListItem item = list.Add(null, changelist.description, changelist);
                item.Exclusive = true;
                item.CanAccept = true;
                item.Icon = icon;
            }
            list.Refresh();
            base.Repaint();
        }
    }
}

